~ 多了一層,價格不變 ~
AWS Lambda Layer功能使得可以單獨上傳並集中管理Lambda的相依套件了,這樣 Lambda 函數打包的時候就只需要打包該函數本身的程式碼,相依套件只需要引用即可。這樣的好處是顯而易見的:
接著我們一步步來示範如何使用 Layer 功能
# Day9/marathon-lambda-layer/Dockerfile
FROM amazonlinux:2.0.20191016.0
RUN yum install -y python37 && \
yum install -y python3-pip && \
yum install -y zip && \
yum clean all
RUN python3.7 -m pip install --upgrade pip && \
python3.7 -m pip install virtualenv
# Apple M1 Macbook default platform is linux/arm64.
$ docker build --platform=linux/amd64 -t lambdalayer:latest .
# bash into your container.
$ docker run -it --name lambdalayer lambdalayer:latest bash
# in container
bash-4.2# python3.7 -m venv pandas
bash-4.2# source pandas/bin/activate
(pandas) bash-4.2# pip install pandas -t ./python
(pandas) bash-4.2# deactivate
bash-4.2# zip -r layer.zip ./python/
bash-4.2# exit
# in the host
$ docker cp lambdalayer:layer.zip .
$ aws lambda publish-layer-version \
--layer-name marathon-layer \
--description "My First Marathon Layer" \
--zip-file fileb://layer.zip \
--compatible-runtimes python3.7 \
--compatible-architectures "x86_64"
# package it to zip format
$ zip function.zip lambda_function.py
# aws cli create-function
# query 'marathon-lambda-ex' role ARN first
$ aws iam get-role --role-name marathon-lambda-ex
$ aws lambda create-function \
--function-name marathon-function-with-layer \
--zip-file fileb://function.zip \
--handler lambda_function.lambda_handler --runtime python3.7 \
--role <marathon-lambda-ex arn>
$ aws lambda invoke --function-name marathon-function-with-layer out.log --log-type Tail
# out.log
{"errorMessage": "Unable to import module 'lambda_function': No module named 'pandas'", "errorType": "Runtime.ImportModuleError", "stackTrace": []}
$ aws lambda update-function-configuration \
--function-name marathon-function-with-layer \
--layers {layer's $arn:$version}
# ex. arn:aws:lambda:ap-northeast-1:411932542528:layer:marathon-layer:1
# can query the "last" version layer's arn with layer's name
$ aws lambda list-layer-versions --layer-name {layer name} \
--max-items 1 --no-paginate --query 'LayerVersions[0].LayerVersionArn' \
--output text
# test
$ aws lambda invoke --function-name marathon-function-with-layer out.log --log-type Tail
# out.log
{"statusCode": 200, "body": "0 2\n1 1\n2 7\n3 3\ndtype: int64"}
我們可以看到使用 lambda layer 可以部署 python package 上去供 lambda function使用,而lambda function 本身的 package 可以不需要包含這個 package,可以突破 lambda function zip 檔案的 50MB 限制,儘管如此,lambda layer 也有一些限制如下:
# for the example, need to upload layer.zip to s3 first
$ aws lambda publish-layer-version \
--layer-name marathon-layer \
--description "My First Marathon Layer" \
--content S3Bucket={Bucket Name},S3Key=layer.zip \
--compatible-runtimes python3.7 \
--compatible-architectures "x86_64"
https://medium.com/towards-data-science/how-to-install-python-packages-for-aws-lambda-layer-74e193c76a91
https://dashbird.io/knowledge-base/aws-lambda/lambda-layers/